package nthu.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import nthu.web.model.domain.Hadoop;
import nthu.web.model.domain.box.MemoBox;

import org.json.JSONException;
import org.json.JSONObject;

@SuppressWarnings("serial")
public class HadoopStorageControll extends ResourceController<byte[]>{
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
		PrintWriter out = response.getWriter();
		Hadoop hadoop = new Hadoop();
		String[] form = new String[4]; 
		form[0] = request.getParameter("id");
		form[1] = request.getParameter("cid");
		form[2] = request.getParameter("name");
		form[3] = request.getParameter("content");
		try{
			MemoBox memo = new MemoBox(Long.valueOf(form[0]), Long.valueOf(form[1]), form[2], form[3]);
			memo.setStamp(System.currentTimeMillis());
			hadoop.storeMemoBox(memo, "bear/"+memo.getStamp()+".xml");
			JSONObject[] json = hadoop.listUsrMemo("bear");
			out.println(json.length);
			out.println(json[0].toString());
			for(JSONObject tmp: json){
				out.println("stamp: "+tmp.getLong("stamp"));
				out.println("name: "+tmp.getString("name"));
				out.println("content: "+tmp.getString("Content"));
				out.println();
			}
		} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
		}
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException{
		
	}
}